package kickboxing.view;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kickboxing.controller.FTPManager;
import kickboxing.model.Uzytkownik;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

public class GaleriaZdjecAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public GaleriaZdjecAction() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesja = request.getSession(true);
		Uzytkownik user = (Uzytkownik) sesja.getAttribute("user");
		FTPManager ftp = new FTPManager();
		String nazwaAlbumu = (String) sesja.getAttribute("nazwaAlbumu");
		
		if(user != null){
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
	        int i=0;
	        if (isMultipart) {
	            FileItemFactory factory = new DiskFileItemFactory();
	            ServletFileUpload upload = new ServletFileUpload(factory);
	            
	            try {
	            	ftp.polaczZFTP();
	            	ftp.wybierzKatalogFTP("gladiator/"+ nazwaAlbumu + "/");
	                List items = upload.parseRequest(request);
	                Iterator iterator = items.iterator();
	                while (iterator.hasNext()) {
	                    FileItem item = (FileItem) iterator.next();
	                    
	                    if (!item.isFormField()) {
	                        String fileName = item.getName();
	 
	                        String root = getServletContext().getRealPath("/");
	                        File path = new File(root + "/uploads");
	                        if (!path.exists()) {
	                            boolean status = path.mkdirs();
	                        }
	                        File uploadedFile = new File(path +"/" + fileName);
	                        System.out.println(uploadedFile.getAbsolutePath());
	                        try{
	                        	item.write(uploadedFile);
		                        ftp.wrzucNaSerwer(uploadedFile, "");
	                        }catch (FileNotFoundException e) {
								System.out.println("Wyjatek " + e);
							}
	                        
	                        
	                    }
	                }
	                ftp.rozlaczZFTP();
	                sesja.removeAttribute("nazwaAlbumu");
                    String message = "Dodano nowa galerie: " + nazwaAlbumu + "!";
        			request.setAttribute("message", message);
        			String x = "?message="+message;
        			response.sendRedirect("admin/adminPanel.jsp"+x);
	            } catch (FileUploadException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
		}else{
			response.sendError(404, "Brak Uprawinien");
		}
	}

}
